<?php 

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	
	protected function _initModules() {
		
      	$this
      		->getApplication()
      		->getAutoloader()
      			->registerNamespace(array('App_', 'Model', 'Form'))
            	->pushAutoloader(array($this, 'loadModel'), array('Model', 'Form'));
      	
		$this->bootstrap('frontController');
		$front = $this->getResource('frontController');
		
		$di = new DirectoryIterator(APPLICATION_PATH . '/modules');
        foreach ($di as $file) {
            if (!$file->isDir() || $file->isDot()) 
                continue;
            
            $moduleDir = $file->getPathname() . DIRECTORY_SEPARATOR . $front->getModuleControllerDirectoryName();
            $front->addControllerDirectory($moduleDir, $file->getFilename());
        }
	}
	
	protected function _initViewHelpers()
	{
	    $this->bootstrap('view');
	    $view = $this->getResource('view');
	    
	    $view->headTitle()->setSeparator(' - ');
    	
	    $view->addHelperPath("ZendX/JQuery/View/Helper", "ZendX_JQuery_View_Helper");
    	$view->addHelperPath("App/View/Helper", "App_View_Helper");
    	
    	$view->jQuery()->enable()->uiEnable()
        	->setLocalPath('/js/jquery-1.5.1.min.js')
        	->setUiLocalPath('/js/jquery-ui-1.8.12.custom.min.js');
        return $view;
	}
	
	
    public function loadModel($class) {
        $parts = explode('_', $class, 3);
        if(empty($parts[2])) {
            return false;
        }
        
        $rewrite = array(
        	'model' => 'models',
        	'form'  => 'forms',
        );
       
        $path = APPLICATION_PATH 
        		. '/modules/' 
        		. strtolower($parts[1]) 
        		. '/'
        		.$rewrite[strtolower($parts[0])]
        		. '/' 
        		. str_replace('_', '/', $parts[2]) . '.php';
                                    
        if(file_exists($path)) {
            require_once $path;
            return true;
        }  
        return false;
    } 
}
